from django.db import models
from db.abstract_model import AbstractModel
from tinymce.models import HTMLField
from utils.wrapper import *


# 服务模型管理类
class ServerTypeManager(models.Manager):
    # 获取所有类型
    def get_all(self):
        try:
            return self.all().order_by("-sum")
        except:
            pass

    # 根据id获取类型
    def get_type_by_id(self,param):
        try:
            return self.get(id=param)
        except:
            pass

    def update_order_sum(self,request):
        type_id = post(request,"user_order")
        s = self.get(id=type_id)
        s.sum += 1
        s.save()


# 服务模型
class ServerType(AbstractModel):
    type = models.CharField(max_length=20, verbose_name="项目类型")
    img = models.ImageField(upload_to="images/server",verbose_name="项目图片")
    sum = models.BigIntegerField(default=0,verbose_name="预约数量")

    objects = ServerTypeManager()

    def __str__(self):
        return self.type

    class Meta:
        verbose_name = "预约项目"
        verbose_name_plural = "预约项目"


# 预约模型管理
class ServerOrderManage(models.Manager):
    def save_order(self,request):
        # 获取留言用户名
        user_name = post(request, "user_name")
        # 获取留言用户联系方式
        user_tel = post(request, "user_tel")
        # 获取预约项目
        user_order = post(request, "user_order")
        # 获取预约时间
        user_time = post(request, "user_time")
        # 获取备注
        user_msg = post(request, "user_msg")
        # 存入数据库
        order = ServerOrder()
        order.user_order_id = user_order
        order.user_name = user_name
        order.user_tel = user_tel
        order.user_time = user_time
        order.user_msg = user_msg
        order.save()



# 预约模型
class ServerOrder(AbstractModel):
    # 预约项目
    user_order = models.ForeignKey(ServerType,verbose_name="预约项目")
    # 留言用户名
    user_name = models.CharField(max_length=20, verbose_name="客户名")
    # 用户联系方式
    user_tel = models.CharField(max_length=30,verbose_name="客户联系方式")
    # 预约时间
    user_time = models.CharField(max_length=30, verbose_name="客户预约时间")
    # 留言信息
    user_msg = models.CharField(max_length=500, verbose_name="客户留言")

    objects = ServerOrderManage()

    class Meta:
        verbose_name = "客户预约"
        verbose_name_plural = "客户预约"


class HotProjectManager(models.Manager):
    # 获取所有项目
    def get_all(self):
        try:
            return self.all().order_by("-level")
        except:
            pass

    # 根据类型获取项目
    def get_by_type(self,type):
        try:
            return self.filter(type_id=type).order_by("-level")
        except:
            pass

    # 根据id获取项目
    def get_project_by_id(self,id):
        try:
            return self.get(id=id)
        except:
            pass

    # 获取最新2个项目
    def get_new_project_2(self):
        try:
            return self.all().order_by("-update_time")[:2]
        except:
            pass

    # 获取最热门3个项目
    def get_hot_project_3(self):
        try:
            return self.all().order_by("-level")[:3]
        except:
            pass


class HotProject(AbstractModel):
    name = models.CharField(max_length=20,verbose_name="热门项目名称")
    img = models.ImageField(upload_to="images/server/hotproject/",verbose_name="图片")
    level = models.IntegerField(default=0)
    short = models.CharField(max_length=200,verbose_name="简介")
    desc = HTMLField(verbose_name="详情")
    type = models.ForeignKey(ServerType,verbose_name="项目类型")

    objects = HotProjectManager()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "热门项目"
        verbose_name_plural = "热门项目"

